package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import pojo.Department;
import service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import java.util.List;

@Controller
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/department")
@Api(tags = "科室管理API", description = "科室信息的增删改查接口")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/list")
    @ApiOperation(value = "查询所有科室列表", notes = "返回所有科室的详细信息")
    public ResponseEntity<List<Department>> list() {
        List<Department> departments = departmentService.findAll();
        return ResponseEntity.ok(departments);
    }

    @GetMapping("/{departmentId}")
    @ApiOperation(value = "根据ID查询科室", notes = "返回指定ID的科室信息，ID不存在时返回404")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "科室ID", paramType = "path", dataType = "Integer", required = true, example = "1")
    })
    public ResponseEntity<Department> get(@PathVariable Integer departmentId) {
        Department department = departmentService.findById(departmentId);
        if (department == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(department);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加科室", notes = "传入科室信息添加新科室，成功返回201 Created")
    public ResponseEntity<Void> add(@RequestBody @ApiParam(name = "department", value = "科室实体对象", required = true) Department department) {
        boolean success = departmentService.save(department);
        return success ?
                ResponseEntity.status(HttpStatus.CREATED).build() :
                ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新科室", notes = "传入完整科室信息更新，成功返回200 OK")
    public ResponseEntity<Void> update(@RequestBody @ApiParam(name = "department", value = "科室实体对象", required = true) Department department) {
        boolean success = departmentService.update(department);
        return success ?
                ResponseEntity.ok().build() :
                ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @DeleteMapping("/delete/{departmentId}")
    @ApiOperation(value = "删除科室", notes = "根据ID删除科室，成功返回204 No Content")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "科室ID", paramType = "path", dataType = "Integer", required = true, example = "1")
    })
    public ResponseEntity<Void> delete(@PathVariable Integer departmentId) {
        boolean success = departmentService.delete(departmentId);
        return success ?
                ResponseEntity.noContent().build() :
                ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/level/{level}")
    @ApiOperation(value = "根据层级查询科室", notes = "返回指定层级的所有科室，层级值：1-一级科室，2-二级科室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "科室层级", paramType = "path", dataType = "Integer", required = true, example = "1")
    })
    public ResponseEntity<List<Department>> listByLevel(@PathVariable Integer level) {
        List<Department> departments = departmentService.findByLevel(level);
        return ResponseEntity.ok(departments);
    }

    @GetMapping("/parent/{parentId}")
    @ApiOperation(value = "根据父科室ID查询", notes = "返回指定父科室下的所有子科室，父ID为0表示根科室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父科室ID", paramType = "path", dataType = "Integer", required = true, example = "0")
    })
    public ResponseEntity<List<Department>> listByParentId(@PathVariable Integer parentId) {
        List<Department> departments = departmentService.findByParentId(parentId);
        return ResponseEntity.ok(departments);
    }
}